package com.ljd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ljd.entity.EduChapter;
import com.ljd.entity.EduVideo;
import com.ljd.entity.vo.ChapterVo;
import com.ljd.entity.vo.VideoVo;
import com.ljd.mapper.EduChapterMapper;
import com.ljd.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljd.service.EduVideoService;
import com.why.handler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ljd
 * @since 2021-12-10
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getChapterAndVideo(String courseId) {
        LambdaQueryWrapper<EduChapter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduChapter::getCourseId, courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapper);

        LambdaQueryWrapper<EduVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getCourseId, courseId);
        List<EduVideo> eduVideos = eduVideoService.list(queryWrapper);


        List<ChapterVo> chapterVos = eduChapters.stream().map(entity -> {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(entity, chapterVo);
            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideos) {
                if (entity.getId().equals(eduVideo.getChapterId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
            return chapterVo;
        }).collect(Collectors.toList());

        return chapterVos;
    }

    @Override
    public void saveChapter(EduChapter chapter) {
        baseMapper.insert(chapter);


    }

    @Override
    public void updateChapter(EduChapter chapter) {
        UpdateWrapper<EduChapter> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",chapter.getId());

        baseMapper.update(chapter, wrapper);
    }

    @Override
    public EduChapter getChapter(String chapterId) {
        EduChapter chapter = baseMapper.selectById(chapterId);
        if(chapter == null){
            return new EduChapter();
        }
        return chapter;
    }

    @Override
    public Boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if(count >0) {
            throw new GuliException(20001, "无法删除");
        }else{
            int delete = baseMapper.deleteById(chapterId);
            return delete>0;
        }
    }
}
